package secretary;

import java.sql.SQLException;

import flexibility.userData;
import guiCode.secretaryScreenCode;
import secretaryGUI.createSecretaryWindow;
import basic.ClientConsole;
import com.sun.rowset.CachedRowSetImpl;

public class assignStudentToCourse {

	private userData assignStudent = new userData();
	private userData ID = new userData();

	public void assignStudentToCourse(ClientConsole secretaryConnection,
			CachedRowSetImpl crs, String tableName, createSecretaryWindow csw,
			String courseID, int semesterReal, int classID, int automatic,
			int semesterID) {

		// this will select all the students (by ID) that are in the class and
		// are qualified for the course
		if (tableName.equals("empty")) {
			// all the student are qualified
			System.out.println("empty");
		} else {
			System.out.println("not empty");
			// there are students who are not qualified
			assignStudent.deleteAll();
			assignStudent.addData(244);
			assignStudent.addData(1);
			assignStudent.addData(classID);
			assignStudent.addData(tableName);
			secretaryConnection.acceptMsg(assignStudent);
			crs = secretaryConnection.getResults();
			// this will add the id number to the ID structure
			try {
				while (crs.next()) {
					ID.addData(crs.getString(1));
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			// this will add the student to the studentsVScourses table
			// first validate if the student is already in the list with those
			// details somehow
			for (int i = 0; i < ID.getLength(); i++) {
				assignStudent.deleteAll();
				assignStudent.addData(245);
				assignStudent.addData(1);
				assignStudent.addData(ID.getData(i));
				assignStudent.addData(courseID);
				assignStudent.addData(semesterReal);
				secretaryConnection.acceptMsg(assignStudent);
				crs = secretaryConnection.getResults();
				try {
					if (crs.next() == false) {
						// student is not in the the table
						assignStudent.deleteAll();
						assignStudent.addData(246);
						assignStudent.addData(0);
						assignStudent.addData(ID.getData(i));
						assignStudent.addData(courseID);
						assignStudent.addData(semesterReal);
						assignStudent.addData(automatic);
						secretaryConnection.acceptMsg(assignStudent);
						crs = secretaryConnection.getResults();
					}

				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
			
		}

	}
}
